#!/usr/bin/env python
# coding=utf-8
# Cron Job 列表

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import util
import crondb
import common
import emailtool

class PublicAjaxHandler(webapp.RequestHandler):
	def process(self):
		psw = self.request.get('psw')
		is_ajax = self.request.get('is_ajax')
		act = self.request.get('act')
		error = 0
		msg = ""
		content = ""

		if psw == 'admin':
			if act == "ajax_edit_title":
				id = self.request.get('id')
				if id:
					id = int(id)
				else:
					id = 0
				if id > 0:
					cron = crondb.getCronInfo(id)
				else:
					cron = False;
				if cron:
					org = self.request.get('org')
					val = self.request.get('val')
					cron.title = val
					crondb.updateCronInfo(cron)
					error = 0
					msg = ""
					content = val
				else:
					error = 1
					msg = "Can not found cron job in database. ID=%d" % id
			elif act == "ajax_edit_url":
				id = self.request.get('id')
				if id:
					id = int(id)
				else:
					id = 0
				if id > 0:
					cron = crondb.getCronInfo(id)
				else:
					cron = False;
				if cron:
					org = self.request.get('org')
					val = self.request.get('val')
					cron.url = val
					crondb.updateCronInfo(cron)
					error = 0
					msg = ""
					content = val
				else:
					error = 1
					msg = "Can not found cron job in database. ID=%d" % id
			elif act == "ajax_edit_sendokmsg":
				id = self.request.get('id')
				if id:
					id = int(id)
				else:
					id = 0
				if id > 0:
					cron = crondb.getCronInfo(id)
				else:
					cron = False;
				if cron:
					org = self.request.get('org')
					val = self.request.get('val')
					cron.sendSuccessMsg = (val == "1")
					crondb.updateCronInfo(cron)
					error = 0
					msg = ""
					if cron.sendSuccessMsg:
						content = '1'
					else:
						content = '0'
				else:
					error = 1
					msg = "Can not found cron job in database. ID=%d" % id
			elif act == "ajax_edit_sendfailmsg":
				id = self.request.get('id')
				if id:
					id = int(id)
				else:
					id = 0
				if id > 0:
					cron = crondb.getCronInfo(id)
				else:
					cron = False;
				if cron:
					org = self.request.get('org')
					val = self.request.get('val')
					cron.sendFailMsg = (val == "1")
					crondb.updateCronInfo(cron)
					error = 0
					msg = ""
					if cron.sendFailMsg:
						content = '1'
					else:
						content = '0'
				else:
					error = 1
					msg = "Can not found cron job in database. ID=%d" % id
			elif act == "ajax_edit_enabled":
				id = self.request.get('id')
				if id:
					id = int(id)
				else:
					id = 0
				if id > 0:
					cron = crondb.getCronInfo(id)
				else:
					cron = False;
				if cron:
					org = self.request.get('org')
					val = self.request.get('val')
					cron.enabled = (val == "1")
					crondb.updateCronInfo(cron)
					error = 0
					msg = ""
					if cron.enabled:
						content = '1'
					else:
						content = '0'
				else:
					error = 1
					msg = "Can not found cron job in database. ID=%d" % id
			elif act == "ajax_send_email":
				sendto = self.request.get('sendto')
				subject = self.request.get('subject')
				body = self.request.get('body')
				if not subject:
					subject = 'untitle';
				if not body:
					body = ' ';
				if sendto:
					emailtool.SendMail(sendto, subject, body)
					error = 0
					msg = "OK Send Success."
					content = ''
				else:
					error = 1
					msg = 'Fail Need [SendTo]'
			else:
				error = 1
				msg = "No action."
				content = ""
			
			self.response.headers["Content-Type"] = "text/plain; charset=utf-8"
			self.response.out.write("""{ "error": %d, "message": "%s", "content": "%s" }""" % (error, common.encodeJsString(msg), common.encodeJsString(content)))

	def get(self):
		self.process()

	def post(self):
		self.process()

	def post(self):
		self.process()

def main():
	application = webapp.WSGIApplication([('/pubajax', PublicAjaxHandler)],
		debug=True)
	util.run_wsgi_app(application)


if __name__ == '__main__':
	main()
